exports.MMElevatorModel = Core.Class.subclass(
{
	classname:'MMElevatorModel',
								   
    initialize: function(elevatorJson) 
    {
    	this._elevatorJson = null;
    	
    	if(elevatorJson) {
    		this._elevatorJson = elevatorJson;
    	} else {
    		this._elevatorJson = {"elevatorId" : 1, "itemId" : "4_1_1", "mallId" : 1, "lastRepaired": 0, "repairStartTime": 0, "upgradeStartTime" : 0, "upgradeState" : 0 };
    	}
    	if(this._elevatorJson.lastRepaired == 0)
    	{
    		this._elevatorJson.lastRepaired = MMGameState.getServerTime();
    		this.updateModel();
    	}
    },
    
    canUpgradeBePurchased: function(currency, cost)
    {
		return MMGameState.getCurrentUser().isValidPurchase(currency, cost);
    },
    
    canUpgradeElevator: function()
    {
    	var currentUpgradeLevel = this.getUpgradeState();
    	var targetLevel = +currentUpgradeLevel + 1;
    	
    	if(this.getItemModel().custom_attributes && this.getItemModel().custom_attributes.upgrade_attributes[targetLevel])
    	{
    		return true;
    	}
    	return false;
    	
    },
    
    upgradeElevator: function()
    {
    	if(!this.canUpgradeElevator())
    	{
    		return;
    	}
		
		var pricing = this.getUpgradePricing();
    	var operation = null;
    	
    	if(pricing.currency == kCurrencyType.kCurrencyTypeNuggets)
    	{
    		operation = kOperationType.kOperationSubtractNuggets;
    	}
    	else
    	{
			operation = kOperationType.kOperationSubtractCoins;
    	} 
    	MMGameState.getCurrentUser().updateCurrency(operation, pricing.cost);
    	
    	
    	//Updating Time
		this.setUpgradeStartTime(MMGameState.getServerTime());
		this.updateModel();
    },
    
    upgradeRemainingTime:function()
	{
		var currentTime = MMGameState.getServerTime();
		var upgradeStartTime = this.getUpgradeStartTime();
		var totalUpgradeTime = this.getTotalUpgradeTime();
		
		var diff = totalUpgradeTime - (currentTime - upgradeStartTime);
		
		if(diff < 0)
		{
			diff = 0;
		}
		
		return diff;
	},
	
	getUpgradeProgressPercentage: function()
	{
		if(this.isUpgradeInProgress())
		{
			var totalUpgradeTime = this.getTotalUpgradeTime();
			var upgradeRemainingTime = this.upgradeRemainingTime();
			var upgradeTimePassed = totalUpgradeTime - upgradeRemainingTime;
			var upgradePercentage = (upgradeTimePassed/totalUpgradeTime)*100;
			return upgradePercentage;
		}	
		return null;
	},
	
	getTotalUpgradeTime: function()
	{
		var currentUpgradeLevel = this.getUpgradeState();
    	var targetLevel = +currentUpgradeLevel + 1;
    	
    	if(this.getItemModel().custom_attributes && this.getItemModel().custom_attributes.upgrade_attributes[targetLevel])
    	{
    		var upgradeAtt = this.getItemModel().custom_attributes.upgrade_attributes[targetLevel];
    		return upgradeAtt.time;
    	}
    	return null;
	},
	
	getUpgradePricing: function()
	{
		var currentUpgradeLevel = this.getUpgradeState();
    	var targetLevel = +currentUpgradeLevel + 1;
    	
    	if(this.getItemModel().custom_attributes && this.getItemModel().custom_attributes.upgrade_attributes[targetLevel])
    	{
    		var pricing = new Object();
    		var upgradeAtt = this.getItemModel().custom_attributes.upgrade_attributes[targetLevel];
    		pricing.cost = upgradeAtt.cost;
    		pricing.currency= upgradeAtt.currency;
    		return pricing;
    	}
    	return null;		
	},
	
    
    upgradeCompleted: function()
    {
    	var currentUpgradeLevel = this.getUpgradeState();
    	var targetLevel = +currentUpgradeLevel + 1;
    	
    	this.setUpgradeState(targetLevel);
    	this.setUpgradeStartTime(0);
    	
    	this.updateModel();
    },
    
    isUpgradeInProgress: function()
    {
    	var upgradeStartTime = this.getUpgradeStartTime();
    	if(upgradeStartTime > 0)
    	{
    		return true;
    	}
    	return false;
    },
    
    updateModel: function()
	{
		MMGameState.getCurrentUser().updateModel();
	},
	
	getLastRepaired: function()
	{
		if(this._elevatorJson)
		{
			return this._elevatorJson.lastRepaired;
		}
		return null;
	},
	
	getTotalRepairTime: function()
	{
		var currentGrade = this.getUpgradeState();
		var itemModel = this.getItemModel();
		if(itemModel.custom_attributes && itemModel.custom_attributes.upgrade_attributes[currentGrade])
		{
			var repairTime = itemModel.custom_attributes.upgrade_attributes[currentGrade].repair_time;
			return repairTime;
		}
		return 3600;
	},
	
	getTotalBreakdownTime: function()
	{
		var currentGrade = this.getUpgradeState();
		var itemModel = this.getItemModel();
		if(itemModel.custom_attributes && itemModel.custom_attributes.upgrade_attributes[currentGrade])
		{
			var breakdownTime = itemModel.custom_attributes.upgrade_attributes[currentGrade].break_down_time;
			return breakdownTime;
		}
		return 3600;
	},
	
	isInBreakdownMode: function()
	{
		return false;
		
		var lastRepaired = this.getLastRepaired();
		var totalBreakdownTime = this.getTotalBreakdownTime();
		var serverTime = MMGameState.getServerTime();
		
		if(this.getRemainingBreakdownTime() == 0)
		{
			return true;
		}		
		return false;
	},
	
	getRemainingBreakdownTime: function()
	{
		var lastRepaired = this.getLastRepaired();
		var totalBreakdownTime = this.getTotalBreakdownTime();
		var serverTime = MMGameState.getServerTime();
		
		var difference = totalBreakdownTime - (serverTime - lastRepaired);
		if (difference > 0)
		{
			return difference;
		}
		return 0;
		
	},
	
	
	setElevatorId: function(elevatorId)
	{
		if(elevatorId && this._elevatorJson)
		{
			this._elevatorJson.elevatorId = elevatorId;
		}
	},
	
	getUpgradeState: function()
	{
		if(this._elevatorJson)
		{
			var upgradeState = this._elevatorJson.upgradeState; 
			return upgradeState;
		}
		return null;
	},
	
	getSpeed: function()
	{
		var currentGrade = this.getUpgradeState();
		var itemModel = this.getItemModel();
		if(itemModel.custom_attributes && itemModel.custom_attributes.upgrade_attributes[currentGrade])
		{
			var speed = itemModel.custom_attributes.upgrade_attributes[currentGrade].speed;
			return speed;
		}
		return 20;
	},
	
	setUpgradeState: function(upgradeState)
	{
		this._elevatorJson.upgradeState = upgradeState; 
	},
	
	getUpgradeStartTime: function()
	{
		if(this._elevatorJson)
		{
			var upgradeStartTime = this._elevatorJson.upgradeStartTime; 
			return upgradeStartTime;
		}
		return null;
	},
	
	setUpgradeStartTime: function(upgradeStartTime)
	{
		this._elevatorJson.upgradeStartTime = upgradeStartTime; 
	},
	
	getItemModel: function()
	{
		var itemModel = GAGameLoader.getStoreHandler().getStoreObjectForKey(this.getItemId());
		return itemModel;
	},
	
	getName: function()
	{
		return this.getItemModel().name;
	},
	
	getElevatorId: function()
	{
		if(this._elevatorJson)
		{
			return this._elevatorJson.elevatorId;
		}
		return null;
	},
	
	setMallId: function(mallId)
	{
		if(mallId && this._elevatorJson)
		{
			this._elevatorJson.mallId = mallId;
		}
	},
	
	getMallId: function()
	{
		if(this._elevatorJson)
		{
			return this._elevatorJson.mallId;
		}
		return null;
	},
	
	setItemId: function(itemId)
	{
		if(itemId && this._elevatorJson)
		{
			this._elevatorJson.itemId = itemId;
		}
	},
	
	getItemId: function()
	{
		if(this._elevatorJson)
		{
			return this._elevatorJson.itemId;
		}
		return null;
	},
    						   
	destroy: function()
	{
		this._elevatorJson = null;	
	},
});